<?php
namespace QuestionPlus\Service\Import\QuestionImport;

use QuestionPlus\Service\Import\QuestionImport\AbstractQuestionImport;

class ChoiceQuestionImport extends AbstractQuestionImport
{
    private $choice = "[A-Za-z]\.\s";

    private $pattern = array(
        'stem' => "【题干】",
        'answer' => "【答案】",
        'difficulty' => "【难度】",
        'score' => "【分数】",
        'analysis' => "【解析】"
    );

    protected function questionFilter ($questionText)
    {
        $pattern = implode('|', $this->pattern);
        $questionText = "【题干】" . $questionText;

        // $isHas = preg_match_all("#({$this->choice}|{$pattern})(([\s\S](?!({$this->choice}|{$pattern})))*[\s\S])#u", $questionText, $matches, PREG_SET_ORDER);
        $isHas = preg_match_all("#({$pattern})(([\s\S](?!({$pattern})))*[\s\S])#u", $questionText, $matches, PREG_SET_ORDER);

        $questionFlags = array_flip($this->pattern);

        $question = array(
            'stem' => "",
            'answer' => "",
            'difficulty' => "一般",
            'score' => "2.0",
            'analysis' => ""
        );

        foreach ($matches as $value) {
            if (array_key_exists($value[1], $questionFlags)) {
                $question[$questionFlags[$value[1]]] = trim($value[2]);
            }
        }

        preg_match_all("#({$this->choice})(([\s\S](?!({$this->choice})))*[\s\S])#u", $question['stem'], $matches, PREG_SET_ORDER);

        foreach ($matches as $value) {
            $question['choices'][] = trim($value[2]);
        }

        $pattern = "#([\s\S]*?)({$this->choice})#";

        preg_match($pattern, $question['stem'], $matches);
        if (!empty($matches[1])) {
            $question['stem'] = $matches[1];
        }

        $question = $this->checkQuestion($question);

        if ($this->questionType == 'uncertain_choice') {
            $question['uncertain'] = "1";
        }

        return $this->choiceFilter($question);
    }

    protected function answerFilter ($answerText)
    {
        $pattern = implode('|', $this->pattern);

        $isHas = preg_match_all("#({$this->choice}|{$pattern})(([\s\S](?!({$this->choice}|{$pattern})))*[\s\S])#u", $answerText, $matches, PREG_SET_ORDER);

        $questionFlags = array_flip($this->pattern);

        $question = array(
            'stem' => "",
            'choices' => '',
            'answer' => "",
            'difficulty' => "一般",
            'score' => "2.0",
            'analysis' => ""
        );

        foreach ($matches as $value) {
            if (array_key_exists($value[1], $questionFlags)) {
                $question[$questionFlags[$value[1]]] = trim($value[2]);
            }
        }

        return $question;

    }

    private function choiceFilter ($question)
    {
        $isHas = preg_match_all("#[A-Za-z]#u", $question['answer'], $matches, PREG_SET_ORDER);
        $question['answer'] = array_map(function($match){
            if (ord($match[0]) < 97) {
                return (string)(ord($match[0]) - 65);
            } else {
                return (string)(ord($match[0]) - 97);
            }
        }, $matches);

        return $this->filter($question);
    }

    protected function testQuestionByType($question)
    {
        if (empty($question['choices'])) {
            $this->throwMessage($question, '选项解析失败');
        } else {
            $choiceCount = count($question['choices']);
            if ($choiceCount < 2) {
                $this->throwMessage($question, '选项数量不能少于两个');
            }

            if ($choiceCount > 10) {
                $this->throwMessage($question, '选项数量不能多于十个');
            }
        }
    }
}